package scm.inventoryControl.managedbean;

import crm.salesOrderProcessing.session.ProductManageSessionBean;
import scm.inventoryControl.entity.ProductBatchRecordEntity;
import scm.inventoryControl.entity.ProductInventoryLevelEntity;
import scm.inventoryControl.session.stateless.InventoryControlSessionBean;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import mrp.productionPlanning.entity.DailyDetailEntity;
import mrp.productionPlanning.session.DailyDetailSessionBean;
import scm.inventoryControl.entity.UnfulfillOrderEntity;

@ManagedBean(name = "createRecordManagedBean")
@ViewScoped
public class CreateRecordManagedBean implements Serializable {

    @EJB
    private ProductManageSessionBean productSessionBean;
    @EJB
    private InventoryControlSessionBean inventorySessionBean;
    @EJB
    private DailyDetailSessionBean dailyDetailSessionBean;
    private List<ProductBatchRecordEntity> productRecordList;
    private ProductBatchRecordEntity productBatchRecord;
    private String productName;
    private ProductInventoryLevelEntity productInventoryLevel;

    public CreateRecordManagedBean() {
        productBatchRecord = new ProductBatchRecordEntity();
        productRecordList = new ArrayList<ProductBatchRecordEntity>();
    }

    public List<String> getProductNames() {
        return productSessionBean.getProductNames();
    }

    public void deleteProductRecord(ProductBatchRecordEntity productBatchRecord) {
        for (int i = 0; i < productRecordList.size(); i++) {
            if (productRecordList.get(i).getProductInventoryLevel().getProduct().getProductName().equals(productBatchRecord.getProductInventoryLevel().getProduct().getProductName())
                    && productRecordList.get(i).getManufactureDate().equals(productBatchRecord.getManufactureDate())) {
                productRecordList.remove(i);
                break;
            }
        }
    }

    public void addProductRecord(ActionEvent event) throws IOException {
        productInventoryLevel = inventorySessionBean.getInventoryByProductName(productName);
        productBatchRecord.setProductInventoryLevel(productInventoryLevel);
        productRecordList.add(productBatchRecord);
        productBatchRecord = new ProductBatchRecordEntity();
    }

    public void submitRecord(ActionEvent event) throws IOException {//save record &  update inventory level
        for (int i = 0; i < getProductRecordList().size(); i++) {
            ProductBatchRecordEntity record = getProductRecordList().get(i);
            String productName = record.getProductInventoryLevel().getProduct().getProductName();
            int recordQuantity = record.getQuantity();
            UnfulfillOrderEntity totalOweQuantity = inventorySessionBean.getTotalOweQuantityByProductName(productName);
            if (totalOweQuantity.getTotalOweQuantity() == 0) {
                inventorySessionBean.saveProductRecord(record);
                inventorySessionBean.adjustInventoryLevel(record);
            } else {

                if (recordQuantity <= totalOweQuantity.getTotalOweQuantity()) {
                    record.setReserved(true);

                    inventorySessionBean.saveProductRecord(record);
                    inventorySessionBean.adjustInventoryLevel2(record);
                    inventorySessionBean.adjustTotalOweQuantity(totalOweQuantity, record);
                } else {//have to split the record
                    ProductBatchRecordEntity oriProductBatchRecord = record;
                    ProductBatchRecordEntity newProductBatchRecord = new ProductBatchRecordEntity();
                    newProductBatchRecord.setQuantity(oriProductBatchRecord.getQuantity() - totalOweQuantity.getTotalOweQuantity());
                    oriProductBatchRecord.setQuantity(totalOweQuantity.getTotalOweQuantity());
                    oriProductBatchRecord.setReserved(true);
                    newProductBatchRecord.setManufactureDate(oriProductBatchRecord.getManufactureDate());
                    newProductBatchRecord.setProductInventoryLevel(oriProductBatchRecord.getProductInventoryLevel());
                    newProductBatchRecord.setWarehouseLocation(oriProductBatchRecord.getWarehouseLocation());
                    inventorySessionBean.saveOriNewRecords(oriProductBatchRecord, newProductBatchRecord);
                    inventorySessionBean.adjustInventoryLevel3(oriProductBatchRecord, newProductBatchRecord);
                    inventorySessionBean.adjustTotalOweQuantity2(totalOweQuantity);
                }
            }
        }
        //connected inventoryLevel to productRecords
        // inventorySessionBean.connectInventoryToRecords(getProductRecordList());
        productRecordList.clear();
        //update incoming level from tmr:
        updateIncomingLevelFromTmrProductionPlan();
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Records has been successfully saved.", ""));
    }

    public void updateIncomingLevelFromTmrProductionPlan() {
        Calendar cal = new GregorianCalendar();
        Date date = cal.getTime();
        cal.add(Calendar.DATE, 1);
        date = cal.getTime();
        DailyDetailEntity tmrEntity = dailyDetailSessionBean.getDailyDetailByDate(date);
        int [] schedule={tmrEntity.getScheduleA(),tmrEntity.getScheduleB(),tmrEntity.getScheduleC(),tmrEntity.getScheduleD()};
//        ProductInventoryLevelEntity inventoryLevelForEach = inventorySessionBean.getInventoryByProductName("Classic Nutri Bar-Nuts");
//        inventoryLevelForEach.setIncomingQty(tmrEntity.getScheduleA());
//        inventorySessionBean.saveEdit(inventoryLevelForEach);//merge back
//       ProductInventoryLevelEntity inventoryLevelForEach2 = inventorySessionBean.getInventoryByProductName("Classic Nutri Bar-Fruits");
//         inventoryLevelForEach2.setIncomingQty(tmrEntity.getScheduleB());
//        inventorySessionBean.saveEdit(inventoryLevelForEach2);
//        ProductInventoryLevelEntity inventoryLevelForEach3 = inventorySessionBean.getInventoryByProductName("Premium Power Bar-Chocolate Nuts");
//       inventoryLevelForEach3.setIncomingQty(tmrEntity.getScheduleC());
//        inventorySessionBean.saveEdit(inventoryLevelForEach3);
//        ProductInventoryLevelEntity inventoryLevelForEach4 = inventorySessionBean.getInventoryByProductName("Premium Power Bar-Redcurrant Fruit");
//       inventoryLevelForEach4.setIncomingQty(tmrEntity.getScheduleD());
//        inventorySessionBean.saveEdit(inventoryLevelForEach4);
             List<ProductInventoryLevelEntity> pl=inventorySessionBean.getAllProductInventories();
        for(int i=0;i<pl.size();i++){
                 pl.get(i).setIncomingQty(schedule[i]);
                 inventorySessionBean.saveEdit(pl.get(i));
             }

    }

    public List<ProductBatchRecordEntity> getProductRecordList() {
        return productRecordList;
    }

    public void setProductRecordList(List<ProductBatchRecordEntity> productRecordList) {
        this.productRecordList = productRecordList;
    }

    public ProductBatchRecordEntity getProductBatchRecord() {
        return productBatchRecord;
    }

    public void setProductBatchRecord(ProductBatchRecordEntity productBatchRecord) {
        this.productBatchRecord = productBatchRecord;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public ProductInventoryLevelEntity getProductInventoryLevel() {
        return productInventoryLevel;
    }

    public void setProductInventoryLevel(ProductInventoryLevelEntity productInventoryLevel) {
        this.productInventoryLevel = productInventoryLevel;
    }
}
